#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

using namespace std;

int main()
{
    sleep(2);
    int shmid = shmget(374583457, 4096, IPC_CREAT | IPC_EXCL|0644);
    if(shmid < 0)
    {
        cout << "shared memory already exists" << endl;
        return 1;
    }
    cout << "共享内存已经创建好，shmid：" << shmid << endl;
    sleep(2);


    char* s = (char*)shmat(shmid, nullptr, 0);
    cout << (void*)s << endl;
    cout << "共享内存已经挂接好" << endl;
    sleep(2);

    int dt = shmdt(s);
    cout << "共享内存解链接" << endl;
    sleep(2);

    int ret = shmctl(shmid, IPC_RMID, nullptr);
    cout << "共享内存已经释放" << endl;

    return 0;
}

// int main()
// {
//     int shmid = shmget(374583457, 4096, IPC_CREAT|IPC_EXCL|0644);
//     cout << shmid << endl;

//     return 0;
// }